package com.loongtech.bi.manager.realTime;

import com.loongtech.bi.entity.realTime.EntityNewUserByDay;
import com.loongtech.bi.manager.system.ConfigManager;
import com.loongtech.core.jpa.manager.ManagerQueryCacheBase;
import com.loongtech.core.util.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 实时数据-新增用户manager
 * 
 * @author marin
 * @date 2015-09-15
 */
@Service
public class NewUserByDayManager extends ManagerQueryCacheBase<EntityNewUserByDay> {
	private static final long serialVersionUID = 1L;

    @Autowired
    private ConfigManager configManager;
	
	// 得到符合平台、渠道、区服条件的查看日新增用户总和
	public String getAllNewUserByConditions(Date lookDate, String queryPlatform, String queryChannel, String queryServer) {
		List<Object[]> oo = findByQuery("select day,sum(new_user) from EntityNewUserByDay where day>=:dayStart and day<:dayEnd and platformid=:platformid and channelid=:channelid and serverid=:serverid and new_user is not null", "dayStart", new Date(Time.getBeginningOfDay(lookDate.getTime())), "dayEnd", new Date(Time.getBeginningOfDay(lookDate.getTime() + Time.DAY)), "platformid", queryPlatform, "channelid", queryChannel, "serverid", queryServer);
		return getResult(oo);
	}
	
	public String getAllMonthNewUserByConditions(Date lookDate, String queryPlatform, String queryChannel, String queryServer) {
        String opendate = configManager.getConfigValueByName(ConfigManager.OPEN_DATE) == null ? "1900-01-01" : configManager.getConfigValueByName(ConfigManager.OPEN_DATE);
        long d = Time.formatDateStr(opendate, Time.TIMEPATTERN_SIMPLE);
        long d1 = Time.getBeginningOfMonth(lookDate.getTime());
        Date dayStart = d > d1 ? new Date(d) : new Date(d1);

		List<Object[]> oo = findByQuery("select day,sum(new_user) from EntityNewUserByDay where day>=:dayStart and day<:dayEnd and platformid=:platformid and channelid=:channelid and serverid=:serverid and new_user is not null", "dayStart", dayStart, "dayEnd", new Date(Time.getBeginningOfDay(lookDate.getTime() + Time.DAY)), "platformid", queryPlatform, "channelid", queryChannel, "serverid", queryServer);
		return getResult(oo);
	}
	
    // 得到符合国家、区服条件的查看日新增用户总和
    public String getAllNewUserByConditionsBytag(Date lookDate, String queryPlatform, String queryChannel, String queryServer, String queryCountry) {
        List<Object[]> oo = findByQuery("select day,sum(new_user) from EntityNewUserByDayByTag where day >= ? and day< ? and platformid=? and channelid=? and serverid=? and tagvalue=? and new_user is not null", new Date(Time.getBeginningOfDay(lookDate.getTime())), new Date(Time.getBeginningOfDay(lookDate.getTime() + Time.DAY)), queryPlatform, queryChannel, queryServer, queryCountry);
        return getResult(oo);
    }

	// 根据查询结果，拼装最终结果
	private String getResult(List<Object[]> oo) {
		String result = null;
		if (oo != null && oo.size() > 0 && oo.get(0)[0] != null) {
			int newuser = 0;
			for(Object[] o : oo) {
				newuser += Integer.parseInt(o[1].toString());
			}
			result = newuser + "";
		}
		
		return result;
	}
	
	public List<Object[]> getAllNewUserRealTimeDatas(Date lookDate) {
		return findByQuery("select serverid,new_user from EntityNewUserByDay where day=:dayStart and platformid=:platformid and channelid=:channelid and serverid != :serverid", "dayStart", new Date(Time.getBeginningOfDay(lookDate.getTime())), "platformid", "0", "channelid", "0", "serverid", "0");
	}
	
	public List<Object[]> getAllPCSNewUserRealTimeDatas(Date lookDate) {
		return findByQuery("select serverid,new_user from EntityNewUserByDay where day=:dayStart and platformid=:platformid and channelid=:channelid and serverid = :serverid", "dayStart", new Date(Time.getBeginningOfDay(lookDate.getTime())), "platformid", "0", "channelid", "0", "serverid", "0");
	}
	
	public List<Object[]> getPaltformNewUserRealTimeDatas(Date lookDate) {
		return findByQuery("select platformid,new_user from EntityNewUserByDay where day=:dayStart and channelid = :channelid and serverid = :serverid", "dayStart", new Date(Time.getBeginningOfDay(lookDate.getTime())), "channelid", "0", "serverid", "0");
	}
	
	public List<Object[]> getChannelNewUserRealTimeDatas(Date lookDate) {
		return findByQuery("select channelid,new_user from EntityNewUserByDay where day=:dayStart and platformid=:platformid and channelid != :channelid and serverid = :serverid", "dayStart", new Date(Time.getBeginningOfDay(lookDate.getTime())), "platformid", "0", "channelid", "0", "serverid", "0");
	}
}
